import { request } from "../../utils/http"
import { isEmpty, questionTypeSelect } from "../../utils/util"

Page({
  data: {
    questionTypeSelect,
    pageData: {},
    currentIndex: 0,
    knowledge: '',
    answers: [],
    question: {},
    userAnswers: [],
    answerResult: null,
    showAnswer: false,
    questionIds: []
  },

  setQuestionData (data) {
    if (!data) {
      data = this.data.pageData
    }
    this.setData({
      answerResult: null,
      showAnswer: false,
      userAnswers: [],
      answers: data.answers || [],
      knowledge: (data.knowledges || [])[0],
      question: { ...data, typeName: !isEmpty(data.type) && questionTypeSelect.getLabel(data.type) }
    }, () => {
      const answerSelectRef = this.selectComponent('#answerSelect')
      answerSelectRef.updateUserSelected(data.achievementQuestion.achievementAnswerList)
    })
  },

  getQuestion (id) {
    wx.showLoading()
    request(`/api/portal/exam/achievementAnswer/details/${id}`).then(data => {
      wx.hideLoading()
      this.setData({
        pageData: data
      })
      this.setQuestionData(data)
    }).catch(async e => {
      await wx.hideLoading()
      wx.showToast({ title: e.message })
    })
  },

  onLoad: function (options) {
    // options = { id: 3040 }
    this.getQuestion(options.id)
  }
})
